package com.ctshk.app.cms.controller;


import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.cms.dto.UserFeedbackDTO;
import com.ctshk.rpc.cms.json.UserFeedBackFileJson;
import com.ctshk.rpc.cms.req.UserFeedbackAddReq;
import com.ctshk.rpc.cms.req.UserFeedbackQueryReq;
import com.ctshk.rpc.cms.service.IUserFeedbackService;
import com.ctshk.rpc.system.service.IFileOSSService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 用户反馈表 前端控制器
 * </p>
 *
 * @author LiZhiming
 * @since 2021-04-23
 */
@Api(value = "用户反馈-app", tags = "用户反馈-app")
@RestController
@RequestMapping("/user-feedback-app")
public class UserFeedbackController extends BaseController {

    private static final String bucketName = "ctshk-cloud-dev";

    @DubboReference
    private IUserFeedbackService userFeedbackService;

    @DubboReference
    private IFileOSSService fileOSSService;

    @ApiOperation(value = "新增用户反馈-app", notes = "新增用户反馈-app")
    @OperationLogAnnotation(operModul = OperationLogModel.CMS, operType = OperationLogType.ADD)
    @PostMapping("/add")
    public Result add(@RequestBody @Valid UserFeedbackAddReq req) {
//        , @RequestParam("files") MultipartFile[] files
//        if (files != null && files.length > 0) {
//            try {
//                List<UserFeedBackFileJson> feedbackImgs = new ArrayList<>();
//                for (int i = 0; i < files.length; i++) {
//                    InputStream is = files[i].getInputStream();
//                    String uuid = UUID.randomUUID().toString();
//                    String name = files[i].getName();
//                    String extensionName = name.substring(name.lastIndexOf(".") + 1);
//                    String url = fileOSSService.uploadFile2OSS(bucketName, is, uuid + extensionName);
//                    UserFeedBackFileJson ufImage = new UserFeedBackFileJson();
//                    ufImage.setUrl(url);
//                    ufImage.setOldName(name);
//                    ufImage.setNewName(uuid + extensionName);
//                    feedbackImgs.add(ufImage);
//                }
//                req.setFeedbackImgs(feedbackImgs);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
        return userFeedbackService.insert(req, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "查询用户反馈-app", notes = "查询用户反馈-app")
    @OperationLogAnnotation(operModul = OperationLogModel.CMS, operType = OperationLogType.QUERY)
    @GetMapping("/queryById/{userFeedbackId}")
    public Result<UserFeedbackDTO> queryById(@PathVariable("userFeedbackId") String userFeedbackId) {
        return userFeedbackService.queryById(userFeedbackId);
    }

    @ApiOperation(value = "用户反馈分页查询-app", notes = "用户反馈分页查询-app")
    @OperationLogAnnotation(operModul = OperationLogModel.CMS, operType = OperationLogType.QUERY)
    @PostMapping("/queryList")
    public PageResponse<UserFeedbackDTO> queryList(@RequestBody @Valid UserFeedbackQueryReq req) {
        return userFeedbackService.queryList(req);
    }
}
